package com.UnionPayCenter.service.serviceImpl;


import com.UnionPayCenter.constant.ConstantInterface;
import com.UnionPayCenter.entity.ChannelEntity;
import com.UnionPayCenter.entity.ConfEntity;
import com.UnionPayCenter.entity.ManagerEntity;
import com.UnionPayCenter.entity.UserIntegralDetailEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.ConfMapper;
import com.UnionPayCenter.mapper.ManagerMapper;
import com.UnionPayCenter.mapper.UserIntegralDetailMapper;
import com.UnionPayCenter.service.IUserIntegralDetailService;
import com.UnionPayCenter.util.CSVUtil;
import com.UnionPayCenter.util.DateFormatUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:
* @Param:
* @return:
* @Author: bite
* @Date: 2019/4/25
*/
@Service
@Transactional
public class UserIntegralDetailServiceImpl implements IUserIntegralDetailService {
    @Autowired
    private UserIntegralDetailMapper detailMapper;
    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private ConfMapper confMapper;

    @Override
    public PageInfo<UserIntegralDetailEntity> findListByUser(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Integer userId = jsonParam.getInteger("userId");
            //页数
            Integer page = jsonParam.getInteger("page");
            if (page == null) {
                page = 1;
            }
            //每页条数
            Integer pageSize = jsonParam.getInteger("pageSize");
            if (pageSize == null) {
                pageSize = 20;
            }
            String startTime = jsonParam.getString("startTime");
            String endTime = jsonParam.getString("endTime");

            PageHelper.startPage(page, pageSize);

            List<UserIntegralDetailEntity> list =  detailMapper.selectListByUser(userId, startTime, endTime);
            PageInfo<UserIntegralDetailEntity> pageList = new PageInfo<>(list);
            return pageList;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public String export(JSONObject jsonParam) throws IOException {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Integer userId = jsonParam.getInteger("userId");
            String startTime = jsonParam.getString("startTime");
            String endTime = jsonParam.getString("endTime");

            List<UserIntegralDetailEntity> list =  detailMapper.selectListByUser(userId, startTime, endTime);

            String[] propertyName = new String[3];
            propertyName[0] = "时间";
            propertyName[1] = "积分数量";
            propertyName[2] = "备注";

            List<List<String>> exportData = new ArrayList<List<String>>();
            List<String> row;
            if(list != null){
                for (UserIntegralDetailEntity entity: list ) {
                    row = new ArrayList<>();
                    row.add(DateFormatUtil.dateToString(entity.getCreateDate()));
                    row.add(entity.getNumber() +"");
                    row.add(entity.getRemarks());
                    exportData.add(row);
                }
            }

            String name = "/DownloadIntegralDetail-" + DateFormatUtil.getYYYY_MM_DD()+".xls";
            CSVUtil.poiExcel(name,"会员积分明细", propertyName, exportData);

            ConfEntity confEntity = confMapper.selectByKey(ConstantInterface.DOMAIN_URL);
            return confEntity.getConfValue() + name;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public PageInfo<UserIntegralDetailEntity> getListByUser(JSONObject jsonParam) {
        Integer userId = jsonParam.getInteger("userId");
        //页数
        Integer page = jsonParam.getInteger("page");
        if (page == null) {
            page = 1;
        }
        //每页条数
        Integer pageSize = jsonParam.getInteger("pageSize");
        if (pageSize == null) {
            pageSize = 20;
        }
        String startTime = jsonParam.getString("startTime");
        String endTime = jsonParam.getString("endTime");

        PageHelper.startPage(page, pageSize);

        List<UserIntegralDetailEntity> list =  detailMapper.selectListByUser(userId, startTime, endTime);
        PageInfo<UserIntegralDetailEntity> pageList = new PageInfo<>(list);
        return pageList;
    }
}
